package org.eclipse.epsilon.emc.rdbms.core.imodeltest;


import org.eclipse.epsilon.emc.rdbms.core.imodel.DBReflexiveModel;
import org.eclipse.epsilon.eol.EolModule;
import org.eclipse.epsilon.eol.exceptions.models.EolModelLoadingException;

public class TestEOL {

	public static void main(String[] args) throws Exception 
	{
		String modelname = null;
		//String modelString = null;
		modelname = "EPSILON";
		String modelString = "";	
		/********************************************************
		 **  For Test Purposes Only
		 **  Build up a dummy epsilon program                       
		 *********************************************************/
		//Basic Select Examples
		//modelString = ("CUSTOMER.all.println();");  //Print all rows from the customer table
		//modelString = ("CUSTOMER.lastName.println();");  //Print the Surname for all rows from the customer table
		//modelString = ("var a = new CUSTOMER;");  //print all columns for the newly created element
		//modelString = ("var a = new CUSTOMER; a.CUSTOMERID.println( );"); //print only the customer ID from the newly created element.
		//modelString =  ("CUSTOMER.all.first(); a.FORENAME.println();");
		
		//Basic Update examples
		//modelString = ("CUSTOMER.Email='Bob';CUSTOMER.all.println();");  //Set All Emails in the Customer Table to be equal to 'Bob'		 
		//modelString = ("var a = new CUSTOMER;a.FORENAME='Bill';CUSTOMER.all.println();");  //Set new elements Forename to be equal to Bill;
	    //modelString = ("var a = new CUSTOMER;a.ADDRESSID=1; CUSTOMER.SURNAME=a.FORENAME;CUSTOMER.all.println();"); //Set the all the Surnames in the CUSTOMER table to be equal to the FORENAME of the new element which has been created.
		//modelString = ("var a = new CUSTOMER; a.FORENAME = 'Phillip'; CUSTOMER.all=a.FORENAME;CUSTOMER.all.println();");  //This is an illegal operation and therefore cannot be done.
		//modelString = ("var a = new CUSTOMER; a.CUSTOMERID = 1;CUSTOMER.all.println();");  //You cannot update the primary key Value.
		//modelString = ("var a = new CUSTOMER; a.FORENAME=a.CUSTOMERID; a.all.println();"); //Sets the value of the new element FORENAME to be the same as the CUSTOMERID.

		//Basic insert examples
		//modelString = ("var a = new CUSTOMER; CUSTOMER.all.println();");  //Create a new element in the CUSTOMER table 	
		//modelString = ("var b = new jhfdbhfddhfd; jhfdbhfddhfd.all.println();");  //This should error as the type jhfdbhfddhfd should not be found.

		//Basic delete examples 
		//modelString = ("var a = new CUSTOMER; var b = new CUSTOMER;CUSTOMER.all.println();delete a; CUSTOMER.all.println();");  //This should create and delete a given element
		//modelString = ("delete CUSTOMER; ");  //This should delete all data from the CUSTOMER table but the table will still be in the database e.g it does not issue a DROP command;


		//Basic Foreign Key Mapping examples. 
		//Now on a personal level I dont like having to put the Foreign Key Name in here because server GUIs can automatically put a FOREIGN Key called something like _GUID which can be a nightmare for developers and being able to read code.
		//modelString  = ("var a  = new CUSTOMER; a.FORENAME = 'Susan'; a.SURNAME='Melbourne';a.AddressID=1; a.FK_CUSTOMER_ADDRESS.HOUSENUMBER;"); //This should join the CUSTOMER to the address table on the newly created element.

		//DBReflexiveModelElement to record in address table  should return element 

		
		//Join self element together
		//Join element with tables together.
		//modelString  = ("var a=new CUSTOMER; a.ADDRESSID='1';a.FK_CUSTOMER_ADDRESS;"); //Join 2 tables together - //Join 2 tables together  Property 'STREETNAME' not found in object
		//modelString  = ("CUSTOMER.FK_CUSTOMER_ADDRESS.println();"); //Join 2 seperate tables together based on the foreign key name.
		//modelString  = ("CUSTOMER.FK_CUSTOMER_ADDRESS.println();"); //Join 2 tables together
		//modelString  = (" var a=new PERSONS;a.FK_PERSONS_MOTHER.println();"); //Join 2 tables together on element only. 						

		
		//Complex EOL Support
		//modelString = ("Customer.all.size().println();");
		//modelString = ("operation Orders getTotalValue() {var total : Real; for (od in self.FK_OrdersDetail_Order) {total = total + od.quantity * od.FK_OrdersDetail_Product.unitPrice;} return total;} for (o in Orders.all) {o.getTotalValue().println();} ");
        modelString = ("for (o in Orders.all) { var customerAddress = o.FK_Orders_Customer.FK_Customer_Address;var orderAddress = o.FK_Order_Address;if (customerAddress <> orderAddress) {( o.orderId).println();}}");
		

		
		
		
		
		/********************************************************
		 **  End Of test Code....
		 **  
		 *********************************************************/
		Object test = executeTestEOLScript(modelname,modelString );
		//test.getClass().getSimpleName();
	}

	
	/**
	 * Returns the result of an Executed EOL command 
	 *  
	 * @param  modelname  This is the name of the EOL model
	 * @param  modelstring This is the EOL command which is to be executed
	 * @return the results of the executed EOL command.
	 */
	public static Object executeTestEOLScript(String modelname, String modelString) throws Exception
	{	
		DBReflexiveModel rModel = new DBReflexiveModel();  //Create a new model
		rModel.setName(modelname); //Set the Model Name
		rModel.load(); //Load the model
		EolModule module = new EolModule(); //Create a new EOL Module.
		module.parse(modelString); 
		module.getContext().getModelRepository().addModel(rModel);
		return module.execute();
	}
}